package com.trf.member.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author 小小唐
 * @Date 2023/3/6-星期一-23:20
 *
 * JWT 令牌实现身份认证
 */
@Component
@ConfigurationProperties(prefix = "trf.jwt.config")
public class JwtUtil {

    //密钥
    private String secretKey;

    //单位秒，默认7天
    private long expires = 60*60*24*7;

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public long getExpires() {
        return expires;
    }

    public void setExpires(long expires) {
        this.expires = expires;
    }

    /**
     * 生成JWT
     * @param id 会员id
     * @param subject 主题
     * @param isLogin 登录
     * @return
     */
    public String createJWT(String id,String subject,Boolean isLogin){
        long nowMillis = System.currentTimeMillis();
        Date now = new Date(nowMillis);
        JwtBuilder builder = Jwts.builder().setId(id)
                .setSubject(subject)
                .setIssuedAt(now)
                .signWith(SignatureAlgorithm.HS256,secretKey)
                .claim("isLogin",isLogin);
        if (expires > 0){
            //expires乘以1000是毫秒转秒
            builder.setExpiration(new Date(nowMillis + expires*1000));
        }
        return builder.compact();
    }

    /**
     * 解析JWT
     * @param jwtToken 令牌
     * @return
     */
    public Claims parseJWT(String jwtToken){
        return Jwts.parser().setSigningKey(secretKey)
                .parseClaimsJws(jwtToken).getBody();
    }
}
